// 用户的心得页面...

import 'package:flutter/material.dart';
import 'package:sharing/resources/code_point.dart';
import 'package:sharing/resources/strings.dart';
import 'package:sharing/resources/styles.dart';
import 'package:sharing/resources/dimens.dart';
import 'package:sharing/ui/experience/classify_navigation.dart';
import 'package:sharing/ui/experience/pub_experience.dart';

import 'package:sharing/ui/experience/person_realize.dart';
import 'package:sharing/ui/widget/book_info.dart';
import 'package:sharing/model/book.dart';
import 'package:sharing/ui/experience/generate_origin_work.dart';
import 'package:sharing/utils/whole_apis.dart';
import 'package:sharing/net/ris_read_apis.dart';
import 'package:sharing/net/net_util.dart';
import 'package:sharing/ui/widget/book_info_widget.dart';
import 'package:sharing/net/ris_read_apis.dart';
import 'package:dio/dio.dart';
import 'package:sharing/utils/whole_apis.dart';
import 'package:shared_preferences/shared_preferences.dart';

class ExperiencePage extends StatefulWidget {
  ExperiencePage({Key key}) : super(key: key);

  _ExperiencePageState createState() => _ExperiencePageState();
}

class _ExperiencePageState extends State<ExperiencePage> {
  final _push_btn_width = 116.0;
  List<Book >_books = [];

  List<Widget> person_realizes = [];
  List<Widget> _book_widgets = [];



  @override
  void initState() {
    // TODO: implement initState
    super.initState();

//    for (int i = 0; i < 5; i++) {
//      person_realizes.add(PersonRealize());
//    }

    // 加载推荐的书籍
    _loadRecommends();
  }

  List<Widget> _body_class(){
    List<Widget> _lists = [];
    _lists .add(ClassifyNavigation());

    for ( int i=0 ; i< _books.length; i++){
      _lists.add(BookInfoWidgetItem(book: _books[i]));
    }

    return _lists;

  }
  @override
  Widget build(BuildContext context) {
    double _width = MediaQuery.of(context).size.width;
    double _left = (_width - _push_btn_width) / 2;
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: Text(
          IdString.experienceTitle,
          style: AppStyles.pageTitleTextStyle,
        ),
        elevation: Dimensions.appBarElevation,
      ),
      body: Stack(
        fit: StackFit.expand,
        children: <Widget>[
          SingleChildScrollView(
            child: Column(
              children: _body_class(),
            ),
          ),
          BottomPubButton(
              onPress: () {
                print("发布按钮?");
                Navigator.of(context)
                    .push(MaterialPageRoute(builder: (context) {
                  return GenerateOriginWork();
                }));
// todo
                // 导航到编辑发布的页面...
//                Navigator.of(context).push(
//                  MaterialPageRoute(
//                    builder: (context) => BookInfo(
//                          book: Book(
//                            name: "这里是书名",
//                            reviews_score: 8.0,
//                            author: "zai",
//                            url:
//                                "https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=e85dd07b7f094b36db921ceb9bf71be4/0b55b319ebc4b7459a5999b2c6fc1e178b821540.jpg",
//                          ),
//                        ),
//                  ),
//                );
              },
              left: _left,
              width: _push_btn_width),
        ],
      ),
    );
  }

  // 加载推荐的书籍
  void _loadRecommends() async {
    String _url = WholeApis.hostName + RisReadApis.recommend;
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String authorization = await prefs.getString("authorization");

    NetUtil.originGet(_url, Options(headers: {"authorization": authorization}),
        (Response response) {
      print("response data ${response.data}");

      var code = response.data["code"];
      var message = response.data["message"];
      if (code == 200 && message !=null ){
        _books.clear();
        _books = Book.fromJson(message);
        setState(() {

        });
      }
    });
  }
}
